/*
 * @(#)PageAnalyzerController.java, 2024/2/21 10:54
 *
 * Copyright (c) 2018-2024, 四川蜀天梦图数据科技有限公司.
 * All rights reserved.
 */
package spring.controller;

import controllers.ArticleCleaner;
import net.oschina.htmlsucker.Article;
import net.oschina.htmlsucker.HtmlSucker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static net.oschina.htmlsucker.HtmlSucker.TEXT_DENSITY_EXTRACTOR;

/**
 * PageAnalyzerController
 *
 * @author fantazy
 * @version 1.0, 2024年02月21日
 */
@Controller
public class PageAnalyzerController
{
    static final int TIMEOUT = 20000;

    @Autowired
    private ArticleCleaner articleCleaner;

    @PostMapping("/extractor.json")
    @ResponseBody
    public Object extractor(@RequestParam("url") String url)
    {
        Map<String, String> ret = new HashMap<>();
        try
        {
            final Article parsedArticle = HtmlSucker.select(TEXT_DENSITY_EXTRACTOR).parse(url, TIMEOUT);
            ret.put("title", parsedArticle.getTitle());
            ret.put("content", articleCleaner.clean(parsedArticle.getContent()));
            return ret;
        }
        catch (IOException e)
        {
            ret.put("code", "500");
            ret.put("data", e.getMessage());
            e.printStackTrace();
            return ret;
        }
    }
}
